﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Categories;
using MpSoft.Library.BUSS.Contents;
using MpSoft.Library.BUSS.Products;

namespace MpSoft.Library.BUSS.Settings
{
    public class SeoSiteInfoBUSS
    {
        public static SeoSiteInfo SeoSite(string TypeControl,string seTitle, string CategorySeName, int ItemID, Category category)
        {
            SeoSiteInfo obj = new SeoSiteInfo();
            if (CategorySeName.ToLowerInvariant().Equals("default") || string.IsNullOrEmpty(CategorySeName))
            {
                obj.Title = SettingBUSS.GetSettingValue("seo.title", "Công Ty CP Công Nghệ MPSOFT Việt Nam");
                obj.Keywords = SettingBUSS.GetSettingValue("seo.keyword", "Công Ty CP Công Nghệ MPSOFT Việt Nam");
                obj.Descriptions = SettingBUSS.GetSettingValue("seo.description", "Công Ty CP Công Nghệ MPSOFT Việt Nam");
                return obj;
            }
            switch (TypeControl)
            {
                case "san-pham":
                    if (ItemID > 0)
                        obj = GetSeoSiteProduct(ItemID, seTitle, obj);
                    else
                        obj = GetSeoSiteCategory(category, obj);
                    return obj;
                case "bai-viet":
                    obj = GetSeoSiteCategory(category, obj);
                    return obj;
                case "system":
                    obj = GetSeoSiteCategory(category, obj);
                    return obj;
                case "tin-tuc":
                    if (ItemID > 0)
                        obj = GetSeoSiteNews(ItemID, seTitle, obj);
                    else
                        obj = GetSeoSiteCategory(category, obj);
                    return obj;
                default:
                    return GetDefault(obj, CategorySeName);
            }
        }

        private static SeoSiteInfo GetDefault(SeoSiteInfo seoSite, string TypeControl)
        {
            switch (TypeControl)
            {

                case "xac-nhan-thanh-toan":
                    seoSite.Title = "Xác nhận hoàn thành đặt hàng";
                    seoSite.Keywords = "Xác nhận hoàn thành đặt hàng";
                    seoSite.Descriptions = "Xác nhận hoàn thành đặt hàng";
                    break;
                case "tham-do-y-kien":
                    seoSite.Title = "Thăm dò ý kiến";
                    seoSite.Keywords = "Thăm dò ý kiến";
                    seoSite.Descriptions = "Thăm dò ý kiến";
                    break;
                case "tim-kiem-tin":
                    string key = string.Empty;
                    if (HttpContext.Current.Session["newsKeyword"] != null)
                        key = HttpContext.Current.Session["newsKeyword"].ToString();
                    seoSite.Title = "Tìm kiếm - " + key;
                    seoSite.Keywords = "Tìm kiếm - " + key;
                    seoSite.Descriptions = "Tìm kiếm - " + key;
                    break;
                case "tim-kiem-san-pham":
                    var keywordsanpham = string.Empty;
                    if (HttpContext.Current.Session["newsKeyword"] != null)
                        keywordsanpham = HttpContext.Current.Session["newsKeyword"].ToString();
                    seoSite.Title = "Tìm kiếm - " + keywordsanpham;
                    seoSite.Keywords = "Tìm kiếm - " + keywordsanpham;
                    seoSite.Descriptions = "Tìm kiếm - " + keywordsanpham;
                    break;
                case "gio-hang":
                    seoSite.Title = "Giỏ hàng";
                    seoSite.Keywords = "Giỏ hàng";
                    seoSite.Descriptions = "Giỏ hàng";
                    break;
            }
            return seoSite;
        }

        private static SeoSiteInfo GetSeoSiteCategory(Category Cateobj, SeoSiteInfo seoSite)
        {
            if (Cateobj != null)
            {
                seoSite.Title = Cateobj.MetaTitle;
                seoSite.Keywords = Cateobj.MetaKeyword;
                seoSite.Descriptions = Cateobj.MetaDescription;
            }
            return seoSite;
        }

        private static SeoSiteInfo GetSeoSiteProduct(int productid, string seTitle, SeoSiteInfo seoSite)
        {
            var prod = ProductBUSS.Product_ByID(productid);
            if(!prod.FormatUrl.Contains(seTitle))
                HttpContext.Current.Response.Redirect(string.Format(prod.FormatUrl, prod.ProductID));
            seoSite.Title = prod.MetaTitle;
            seoSite.Keywords = prod.MetaKeyword;
            seoSite.Descriptions = prod.MetaKeyword;
            return seoSite;
        }

        private static SeoSiteInfo GetSeoSiteNews(int newsid, string seTitle, SeoSiteInfo seoSite)
        {
            var news = NewsBUSS.News_ByID(newsid);
            if (!news.FormatUrl.Contains(seTitle))
                HttpContext.Current.Response.Redirect(string.Format(news.FormatUrl, news.NewsID));
            seoSite.Title = news.MetaTitle;
            seoSite.Keywords = news.MetaKeyword;
            seoSite.Descriptions = news.MetaKeyword;
            return seoSite;
        }
    }
}
